package cn.neusoft.controller;

import cn.neusoft.pojo.CartItem;
import cn.neusoft.query.CartItemQuery;
import cn.neusoft.service.ICartItemService;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartItemController {
	@Autowired
	private ICartItemService cartItemService;
	@PostMapping      //  请求方式 + 请求路径   post  /cartItem
	public ResultBean save(@RequestBody CartItem cartItem) throws Exception {
		System.out.println("----CartItemController-----add--------");
		if(cartItem.getCartId()==null) {
			return cartItemService.add(cartItem);
		}
		return  cartItemService.update(cartItem);
	}
//	@PutMapping
//	public ResultBean upddate(@RequestBody CartItem cartItem) throws Exception {
//		System.out.println("----CartItemController-----update--------");
//		return cartItemService.update(cartItem);
//	}
	@DeleteMapping("/{id}")
	public ResultBean delete(@PathVariable("id") Integer id) throws Exception {
		System.out.println("----CartItemController-----delete--------");
		return cartItemService.delete(id);
	}
	@GetMapping("/{id}")
	public ResultBean getById (@PathVariable("id") Integer id) throws Exception {
		System.out.println("----CartItemController-----getById--------");
		return cartItemService.getById(id);
	}
	@GetMapping("/all")
	public ResultBean getAll ( ) throws Exception {
		System.out.println("----CartItemController-----getAll--------");
		return cartItemService.getAll();
	}
	@PostMapping("/page")
	public ResultBean getByPage(@RequestBody CartItemQuery cartItemQuery) throws  Exception {
		System.out.println("----CartItemController-----getByPage--------");
		System.out.println(cartItemQuery);
		return  cartItemService.getByPage(cartItemQuery);
	}
	@GetMapping("/batch/{ids}")
	public ResultBean batchDel(@PathVariable("ids") String ids) throws Exception {
		System.out.println("----CartItemController-------batchDel------");
		System.out.println(ids);
		//接收的是一个字符串
		String [] strings=ids.split(",");
		List<Integer> integerList=new ArrayList<>();
		for (String str:strings) {
			integerList.add(Integer.parseInt(str));
		}
		return cartItemService.batchDel(integerList);
	}
}
